/*
 * DesktopApplication2View.java
 */
package desktopapplication2;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.geotools.filter.text.cql2.CQLException;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import javax.swing.JDialog;
import javax.swing.JFrame;
import marcin.belzowski.geoMap.Queries;
import marcin.belzowski.gui.StreetsWindow;

/**
 * The application's main frame.
 */
public class DesktopApplication2View extends FrameView {

    public DesktopApplication2View(SingleFrameApplication app) throws Exception {
        super(app);

        initComponents();
        initializeStreetMapsAndDraw();
        labelInfo.setText("Zaznacz ulicę, aby zobaczyć jej opis");
        queries = new Queries();
        initStreesCombo();



        /////////////////////////////////////////////////////

//       
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = DesktopApplication2.getApplication().getMainFrame();
            aboutBox = new DesktopApplication2AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        DesktopApplication2.getApplication().show(aboutBox);
    }

    public void initInfoText() {
        String s = "<html>";


        if (streetWindow == null) {
            return;
        }
        ArrayList<String> streets = streetWindow.getStreetsSelected();
        if (streets.size() == 0) {
            labelInfo.setText("Zaznacz ulicę, aby zobaczyć jej opis");
            return;
        }

        for (String str : streets) {

            s += str;
            s += "<br />";
            s += "Crowd:    ";
            String[] l = queries.findFrequency(str).toString().split("\\.");

            s += l[0];
            if (l.length > 1 && l[1].length() > 2) {
                s += "." + l[1].substring(0, 3);
            }
            s += " cars / hours";
            s += "<br />Distance:";
            s += queries.findDistance(str).toString().split("\\.")[0];
            s += " <br /><br />";
        }
        s += "</html>";
        labelInfo.setText(s);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mapPane = new org.geotools.swing.JMapPane();
        refreshButton = new javax.swing.JButton();
        labelInfo = new javax.swing.JLabel();
        chooseStreetsLabel = new javax.swing.JLabel();
        streetsCombo = new javax.swing.JComboBox();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        statistics = new javax.swing.JMenu();
        table = new javax.swing.JMenuItem();
        chartMenu = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mapPane.setName("mapPane"); // NOI18N
        mapPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mapPaneMouseClicked(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(desktopapplication2.DesktopApplication2.class).getContext().getActionMap(DesktopApplication2View.class, this);
        refreshButton.setAction(actionMap.get("drawStreetForColorFrequency")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication2.DesktopApplication2.class).getContext().getResourceMap(DesktopApplication2View.class);
        refreshButton.setText(resourceMap.getString("refreshButton.text")); // NOI18N
        refreshButton.setName("refreshButton"); // NOI18N

        javax.swing.GroupLayout mapPaneLayout = new javax.swing.GroupLayout(mapPane);
        mapPane.setLayout(mapPaneLayout);
        mapPaneLayout.setHorizontalGroup(
            mapPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mapPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(refreshButton)
                .addContainerGap(497, Short.MAX_VALUE))
        );
        mapPaneLayout.setVerticalGroup(
            mapPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mapPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(refreshButton)
                .addContainerGap(268, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(mapPane);

        labelInfo.setFont(resourceMap.getFont("labelInfo.font")); // NOI18N
        labelInfo.setText(resourceMap.getString("labelInfo.text")); // NOI18N
        labelInfo.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        labelInfo.setAlignmentX(1.0F);
        labelInfo.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        labelInfo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        labelInfo.setName("labelInfo"); // NOI18N
        labelInfo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelInfoMouseClicked(evt);
            }
        });

        chooseStreetsLabel.setFont(resourceMap.getFont("streetsCombo.font")); // NOI18N
        chooseStreetsLabel.setText(resourceMap.getString("chooseStreetsLabel.text")); // NOI18N
        chooseStreetsLabel.setName("chooseStreetsLabel"); // NOI18N

        streetsCombo.setFont(resourceMap.getFont("streetsCombo.font")); // NOI18N
        streetsCombo.setName("streetsCombo"); // NOI18N
        streetsCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                streetsComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 580, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(labelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(89, 89, 89)
                        .addComponent(chooseStreetsLabel)
                        .addGap(27, 27, 27)
                        .addComponent(streetsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(321, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(streetsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(chooseStreetsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(labelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(92, 92, 92))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        statistics.setText(resourceMap.getString("statistics.text")); // NOI18N
        statistics.setName("statistics"); // NOI18N

        table.setAction(actionMap.get("showTables")); // NOI18N
        table.setText(resourceMap.getString("table.text")); // NOI18N
        table.setName("table"); // NOI18N
        statistics.add(table);

        chartMenu.setAction(actionMap.get("showChartWindow")); // NOI18N
        chartMenu.setText(resourceMap.getString("chartMenu.text")); // NOI18N
        chartMenu.setName("chartMenu"); // NOI18N
        statistics.add(chartMenu);

        menuBar.add(statistics);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void mapPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapPaneMouseClicked

        initInfoText();


    }//GEN-LAST:event_mapPaneMouseClicked

    private void labelInfoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelInfoMouseClicked
}//GEN-LAST:event_labelInfoMouseClicked

    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_aboutMenuItemActionPerformed

    private void streetsComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_streetsComboActionPerformed
        try {
            String selected = (String) streetsCombo.getSelectedItem();
            streetWindow.selectFeatures(selected);
            
            initInfoText();
        } catch (CQLException ex) {
            Logger.getLogger(DesktopApplication2View.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_streetsComboActionPerformed
    private void initializeStreetMapsAndDraw() {
        if (mapPane == null) {
            return;
        }
        streetWindow = new StreetsWindow(mapPane);
        try {
            streetWindow.showMap();

        } catch (Exception ex) {
            Logger.getLogger(DesktopApplication2View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initStreesCombo() {
        ArrayList<String> list = queries.getStreets();
        for (String s : list) {
            streetsCombo.addItem(s);
        }

    }

    @Action
    public void showTables() {
//        if (tablesWidnow == null) {
            JFrame mainFrame = DesktopApplication2.getApplication().getMainFrame();
            tablesWidnow = new StatisticsTable(mainFrame, true);
            tablesWidnow.setLocationRelativeTo(mainFrame);
//        }
        DesktopApplication2.getApplication().show(tablesWidnow);
    }

    @Action
    public void drawStreetForColorFrequency() {
        frequency = queries.findFrequencyforAll(queries.getStreets());
        Double[] d = new Double[frequency.size()];
        d = frequency.values().toArray(d);
        Arrays.sort(d);

        for (String s : frequency.keySet()) {
            if (frequency.get(s) == d[0]) {
                min = s;
            }
            if (frequency.get(s) == d[d.length - 1]) {
                max = s;
            }

        }
        try {
            streetWindow.selectFeatures(max, min);
        } catch (CQLException ex) {
            Logger.getLogger(DesktopApplication2View.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void drawNotRecalculate() {
        try {
            streetWindow.selectFeatures(max, min);
        } catch (CQLException ex) {
            Logger.getLogger(DesktopApplication2View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Action
    public void showChartWindow() {
        if (chartWindow == null) {
            JFrame mainFrame = DesktopApplication2.getApplication().getMainFrame();
            chartWindow = new ChartWindow(mainFrame, true);
            chartWindow.setLocationRelativeTo(mainFrame);
        }
        DesktopApplication2.getApplication().show(chartWindow);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem chartMenu;
    private javax.swing.JLabel chooseStreetsLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelInfo;
    private javax.swing.JPanel mainPanel;
    private org.geotools.swing.JMapPane mapPane;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton refreshButton;
    private javax.swing.JMenu statistics;
    private javax.swing.JComboBox streetsCombo;
    private javax.swing.JMenuItem table;
    // End of variables declaration//GEN-END:variables
    Hashtable<String, Double> frequency;
    private StreetsWindow streetWindow;
    private Queries queries;
    private JDialog aboutBox;
    private JDialog tablesWidnow;
    private JDialog chartWindow;
    String min = "";
    String max = "";
}
